package cn.weekimwee.firstlineofcodeforkotlin.unit9

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import cn.weekimwee.firstlineofcodeforkotlin.unit9.network.HttpManager
import cn.weekimwee.firstlineofcodeforkotlin.unit9.network.bindThreadAndLiftCircle
import io.reactivex.rxkotlin.subscribeBy
import kotlinx.android.synthetic.main.activity_start_retrofit.*

class StartRetrofitActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start_retrofit)
        HttpManager.somethingService.stainGodApi(1)
                .bindThreadAndLiftCircle(this@StartRetrofitActivity)
                .subscribeBy(
                        onNext = {
                            it.handleResult { data ->
                                data?.apply {
                                    val str = StringBuilder()
                                    forEach {
                                        str.append(it)
                                    }
                                    text.text = str
                                }
                            }
                        },
                        onError = {
                            it.printStackTrace()
                        }
                )
    }
}
